<?php include('scripts.php'); ?>
<script type="text/javascript">
var packageStrings = "<option value=''></option>";
var datagrid = null;
var datagridAnnouncement = null;
$(function() {
    datagrid = $("#datagridAnnouncement").r9jasonDataTable({
        // online1DataTable extra params
        "idTr" : true, // assign <tr id='xxx'> from 1st columns array(aoColumns);
        "extraParam" : function(aoData) { // pass extra params to server

        },
        "reassignEvent" : function() { // extra function for reassignEvent when JSON is back from server
            reassignDatagridAnnouncementEventAttr();
        },
        // datatables params
        "bLengthChange": true,
        "bFilter": false,
        "bProcessing": true,
        "bServerSide": true,
        "bAutoWidth": false,
        "sAjaxSource": "/member/announcementList",
        "sPaginationType": "full_numbers",
        "aaSorting": [
            [4,'desc']
        ],
        "aoColumns": [
            { "sName" : "announcement_id",  "bVisible": false},
            { "sName" : "title",  "bSortable": false, "fnRender": function (oObj) {
                return "<a class='announcementLink' refId='" + oObj.aData[0] + "' href='#' style='color: #0000ff;'>" + oObj.aData[1] + "</a>";
            }},
            { "sName" : "created_on",  "bSortable": false}
        ]
    });

    $(".deleteLink").button({
        icons: {
            primary: "ui-icon-circle-close"
        }
    });
    $(".activeLink").button({
        icons: {
            primary: "ui-icon-circle-check"
        }
    }).click(function(event) {
        event.preventDefault();
        waiting();
        $("#distributorId").val($(this).attr("ref"));
        $("#dgActivateMember_shareholderId").val($(this).attr("refCode"));
        $("#dgActivateMember_alias").val($(this).attr("refNickname"));
        $("#dgActivateMember_registeredTime").val($(this).attr("refCreatedDate"));
        $.ajax({
            type : 'POST',
            url : "/member/fetchPackage",
            dataType : 'json',
            cache: false,
            data: {
            },
            success : function(data) {
                $.unblockUI();
                packageStrings = "";
                jQuery.each(data.package, function(key, value) {
                    packageStrings += "<option value='" + value.price + "' ref='" + value.packageId + "'>" + value.name + "</option>";
                });

                $("#dgActivateMember").dialog("open");

                $("#dgActivateMember_point").html(packageStrings).trigger("change");

                $("#dgActivateMember_pointAvail").val(data.point);
                $("#dgActivateMember_ecash").val(data.ecash);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert("Server connection error.");
            }
        });
    });

    $("#dgActivateMember").dialog("destroy");
    $("#dgActivateMember").dialog({
        autoOpen : false,
        modal : true,
        resizable : false,
        hide: 'clip',
        show: 'slide',
        width: 400,
        buttons: {
            "<?php echo __('Cancel') ?>": function() {
                $(this).dialog('close');
            },
            "<?php echo __('Submit') ?>": function() {
                if ($("#dgActivateMember_point").val() == 0 || $("#dgActivateMember_point").val() == "") {
                    error("e-Point cannot be zero.");
                    $("#dgActivateMember_point").focus().select();
                } else {
                    if ($.trim($("#dgActivateMember_transactionPassword").val()) == "") {
                        error("Transaction Password is empty");
                        $("#dgActivateMember_transactionPassword").focus();
                        return false;
                    }
                    waiting();
                    $.ajax({
                                type : 'POST',
                                url : "/member/activateMember",
                                dataType : 'json',
                                cache: false,
                                data: {
                                    packageId : $('#dgActivateMember_point option:selected').attr("ref")
                                    , transactionPassword : $('#dgActivateMember_transactionPassword').val()
                                    , sponsorId : $('#distributorId').val()
                                },
                                success : function(data) {
                                    if (data.error == false) {
                                        $.unblockUI();
                                        window.location = "<?php echo url_for('/member/summary') ?>";
                                    } else {
                                        error(data.errorMsg);
                                        $("#dgActivateMember_transactionPassword").focus().select();
                                    }
                                },
                                error : function(XMLHttpRequest, textStatus, errorThrown) {
                                    error("Server connection error.");
                                }
                            });
                }
            }
        },
        open: function() {

        },
        close: function() {

        }
    });

$("#dgActivateMember_point").change(function() {
$("#dgActivateMember_pointNeeded").val($("#dgActivateMember_point").val());
});

$("#dgAnnouncement").dialog("destroy");
$("#dgAnnouncement").dialog({
        autoOpen : false,
        modal : true,
        resizable : false,
        hide: 'clip',
        show: 'slide',
        width: 700,
        height: 500,
        buttons: {
            "<?php echo __('Cancel') ?>": function() {
                $(this).dialog('close');
            }
        },
        open: function() {

        },
        close: function() {

        }
    });

    $("#reinvestGapLink").live("click", function(event) {
        event.preventDefault();
        $("#dgReinvestCps").dialog("open");
    });
    $(".announcementLink").live("click", function(event) {
        event.preventDefault();
        $("#dgAnnouncement").data("refId", $(this).attr("refId"));

        waiting();
        $.ajax({
            type : 'POST',
            url : "/member/fetchAnnouncementById",
            dataType : 'json',
            cache: false,
            data: {
                announcementId : $('#dgAnnouncement').data("refId")
            },
            success : function(data) {
                $.unblockUI();
                $("#dgAnnouncement").dialog("open");
                $("#tdAnnouncement").html("<strong>" + data.content + "</strong>");
                $("#dgAnnouncement").dialog("option", "title", data.title);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                error("Your login attempt was not successful. Please try again.");
            }
        });
    });

    /*$.ajax({
        type : 'POST',
        url : "/member/fetchAnnouncementById",
        dataType : 'json',
        cache: false,
        data: {
            announcementId : 11
        },
        success : function(data) {
            $.unblockUI();
            $("#dgAnnouncement").dialog("open");
            $("#tdAnnouncement").html("<strong>" + data.content + "</strong>");
            $("#dgAnnouncement").dialog("option", "title", data.title);
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            alert("Your login attempt was not successful. Please try again.");
        }
    });*/
});
function reassignDatagridAnnouncementEventAttr() {

}
</script>

<div style="margin: 10px 20px 10px 20px;">
    <p><strong><?php echo $fullName?></strong>, welcome to Vital Universe.</p>
    <table class="tablelist" style="padding-bottom: 10px;" cellpadding="0" cellspacing="0">
        <tbody>
        <tr>
            <td valign="top" width="60%">
                <table cellpadding="0" cellspacing="0" width="100%">
                    <tbody>
                    <tr>
                        <td class="caption">
                            Package:
                        </td>
                        <td class="value">
                            <?php echo $packageName?>
                        </td>
                    </tr>
                    <tr>
                        <td class="caption">
                            Pip:
                        </td>
                        <td class="value">
                            <?php echo $pips?>
                        </td>
                    </tr>
                    <tr>
                        <td class="caption">
                            Commission:
                        </td>
                        <td class="value">
                            <?php echo $commission?> %
                        </td>
                    </tr>
                    <tr>
                        <td class="caption">
                            Direct Sponsors:
                        </td>
                        <td class="value">
                            <?php echo $directSponsor?>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </td>
            <td valign="top" width="4%">&nbsp;</td>
            <td valign="top" width="36%">
                <!--<table class="tableActivity" cellpadding="0" cellspacing="0" width="100%">
                    <tbody>
                    <tr>
                        <th colspan="2">Announcements</th>
                    </tr>
                    <?php
/*                    if (count($announcements) > 0) {
                        foreach ($announcements as $announcement) {
                    */?>
                    <tr>
                        <td class="tdlist"><span class="date"><?php /*echo $announcement->getCreatedOn()*/?></span><br><span class="text"><?php /*echo $announcement->getCreatedOn()*/?></span>
                        </td>
                    </tr>
                    <?php /*} */?>
                    </tbody>
                </table>-->
            </td>
        </tr>
        </tbody>
    </table>
</div>

<form action="/member/activateMember" method="post" id="memberForm">
    <input type="hidden" id="distributorId">

    <div style="padding: 10px; top: 30px; width: 98%">

        <div class="portlet">
            <div class="portlet-header"><?php echo __('Inactive Shareholders') ?></div>
            <div class="portlet-content">

                <table class="display" id="datagrid" border="0" width="100%" cellpadding="0" cellspacing="0">
                    <thead>
                    <tr>
                        <th><?php echo __('Shareholder ID') ?></th>
                        <th><?php echo __('Alias') ?></th>
                        <th><?php echo __('Registered Time') ?></th>
                        <th><?php echo __('Action') ?></th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php
                    if (count($pendingDistributors) > 0) {
                        $trStyle = "even";
                        foreach ($pendingDistributors as $dist) {
                            if ($trStyle == "even") {
                                $trStyle = "odd";
                            } else {
                                $trStyle = "even";
                            }

                            echo "<tr class='" . $trStyle . "'>
				        <td align='center'>" . $dist->getDistributorCode() . "</td><td align='center'>" . $dist->getNickname() . "</td>
				        <td align='center'>" . $dist->getCreatedOn() . "</td>
				        <td align='center'>" . link_to(__('Active'), '#', array(
                                                                               'class' => 'activeLink',
                                                                               'ref' => $dist->getDistributorId(),
                                                                               'refCode' => $dist->getDistributorCode(),
                                                                               'refNickname' => $dist->getFullname(),
                                                                               'refCreatedDate' => $dist->getCreatedOn(),
                                                                          )) . "&nbsp;" . link_to(__('Delete'), 'member/delete?distid=' . $dist->getDistributorId(), array(
                                                                                                                                                                          'class' => 'deleteLink',
                                                                                                                                                                          'confirm=Are you sure you want to remove?'
                                                                                                                                                                     )) . "</td></tr>";
                        }
                    } else {
                        echo "<tr class='odd' align='center'><td colspan='4'>" . __('No data available in table') . "</td></tr>";
                    }
                    ?>
                    </tbody>
                </table>
            </div>
        </div>

        <div class="portlet">
            <div class="portlet-header"><?php echo __('Announcements') ?></div>
            <div class="portlet-content">
                <table class="display" id="datagridAnnouncement" border="0" width="100%" cellpadding="0" cellspacing="0">
                    <thead>
                    <tr>
                        <th>Announcement Id[hidden]</th>
                        <th><?php echo __('Title') ?></th>
                        <th width="20%"><?php echo __('Date') ?></th>
                    </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>
</form>


<div id="dgActivateMember" title="<?php echo __('Activate Shareholder') ?>" style="display:none;">
    <input type="hidden" id="dgActivateMember_ecash">
    <input type="hidden" id="dgActivateMember_pointAvail"/>
    <table cellspacing="5" cellpadding="3">
        <tr>
            <td class="text" width="30%"><label><?php echo __('Shareholder ID') ?></label></td>
            <td>:</td>
            <td><input type="text" disabled="disabled" id="dgActivateMember_shareholderId" class="text ui-widget-content ui-corner-all"/></td>
        </tr>
        <tr>
            <td class="text"><label><?php echo __('Full Name') ?></label></td>
            <td>:</td>
            <td><input type="text" disabled="disabled" id="dgActivateMember_alias" class="text ui-widget-content ui-corner-all"/></td>
        </tr>
        <tr>
            <td class="text"><label><?php echo __('Registered Time') ?></label></td>
            <td>:</td>
            <td><input type="text" disabled="disabled" id="dgActivateMember_registeredTime" class="text ui-widget-content ui-corner-all"/></td>
        </tr>
        <tr>
            <td class="text"><label><?php echo __('Package Type') ?></label></td>
            <td>:</td>
            <td>
                <select name="dgActivateMember_point" id="dgActivateMember_point" class='text ui-widget-content ui-corner-all'>

                </select>
                <input type="text" disabled="disabled" id="dgActivateMember_pointNeeded" class="text ui-widget-content ui-corner-all" size="10px"/>
            </td>
        </tr>
        <tr>
            <td class="text"><label><?php echo __('Transaction Password') ?></label></td>
            <td>:</td>
            <td><input type="password" id="dgActivateMember_transactionPassword" class="text ui-widget-content ui-corner-all"/></td>
        </tr>
    </table>
</div>

<div id="dgAnnouncement" title="<?php echo __('Announcements') ?>" style="display:none;">
    <table cellspacing="5">
        <tr>
            <td class="text" id="tdAnnouncement"></td>
    </table>
</div>